<template>
  <el-container class="layout-container-demo" style="height: 500px">
    <!-- 当 isVertical 为 true 时， is-vertical 类名会被添加到 el-aside 元素上，并设置宽度为 250px，高度为 100vh - 60px，即整个页面的高度减去顶部和底部的高度-->
    <transition name="aside-menu">
      <el-aside v-show="isVertical" class="shadow-md">
        <el-menu
            default-active="2"
            class="el-menu-demo"
            mode="vertical"
            :router="false"
            :collapse="isCollapsed"
            :collapse-transition="false"
            :unique-opened="true"
            @open="handleOpen"
            @close="handleClose"
        >

          <!-- 侧边栏 logo -->
          <el-affix>
            <el-collapse-transition>
              <!-- 侧边栏logo,动画效果必须和菜单同步，否则会出现闪烁。参考 vue-pure-admin -->
              <div v-show="true">
                <!-- 关于 router-link 的使用，参考 https://router.vuejs.org/zh/guide/advanced/extending-router-link.html -->
                <router-link :to="'/'">
                  <el-menu-item index="0" style="height: 60px;">
                    <el-icon :size="30">
                      <svg-icon name="app-icon"></svg-icon>
                    </el-icon>
                    <template #title>
                      <span class="ml-4 text-2xl font-bold">龙茶清欢</span>
                    </template>
                  </el-menu-item>
                </router-link>
              </div>
            </el-collapse-transition>
          </el-affix>

          <el-sub-menu index="1">
            <template #title>
              <el-icon><location /></el-icon>
              <span>Navigator One</span>
            </template>
            <el-menu-item-group>
              <template #title><span>Group One</span></template>
              <el-menu-item index="1-1">item one</el-menu-item>
              <el-menu-item index="1-2">item two</el-menu-item>
            </el-menu-item-group>
            <el-menu-item-group title="Group Two">
              <el-menu-item index="1-3">item three</el-menu-item>
            </el-menu-item-group>
            <el-sub-menu index="1-4">
              <template #title><span>item four</span></template>
              <el-menu-item index="1-4-1">item one</el-menu-item>
            </el-sub-menu>
          </el-sub-menu>
          <el-menu-item index="2">
            <el-icon><icon-menu /></el-icon>
            <template #title>Navigator Two</template>
          </el-menu-item>
          <el-menu-item index="3" disabled>
            <el-icon><document /></el-icon>
            <template #title>Navigator Three</template>
          </el-menu-item>
          <el-menu-item index="4">
            <el-icon><setting /></el-icon>
            <template #title>Navigator Four</template>
          </el-menu-item>
        </el-menu>
      </el-aside>
    </transition>
    <el-container>
      <el-header :class="{'is-horizontal': !isVertical}" style="text-align: right; font-size: 12px; padding: 0; height: 60px; line-height: 60px;">
        <transition name="header-menu">
          <el-menu
            v-show="!isVertical"
            :router="false"
            class="el-menu-demo"
            mode="horizontal"
            :default-active="activeIndex"
            :ellipsis="!isVertical"
            :popper-offset="16"
            style="max-width: 60%"
            @select="handleSelect"
          >

            <el-menu-item index="0" @click="toggleDirection">
              <svg-icon name="login-logo" size="30"></svg-icon>
              <span>龙茶清欢</span>
            </el-menu-item>

            <el-sub-menu index="1">
            <template #title>
              <el-icon><message /></el-icon>Navigator One
            </template>
            <el-menu-item-group>
              <template #title>Group 1</template>
              <el-menu-item index="1-1">Option 1</el-menu-item>
              <el-menu-item index="1-2">Option 2</el-menu-item>
            </el-menu-item-group>
            <el-menu-item-group title="Group 2">
              <el-menu-item index="1-3">Option 3</el-menu-item>
            </el-menu-item-group>
            <el-sub-menu index="1-4">
              <template #title>Option4</template>
              <el-menu-item index="1-4-1">Option 4-1</el-menu-item>
            </el-sub-menu>
          </el-sub-menu>
            <el-sub-menu index="2">
            <template #title>
              <el-icon><icon-menu /></el-icon>Navigator Two
            </template>
            <el-menu-item-group>
              <template #title>Group 1</template>
              <el-menu-item index="2-1">Option 1</el-menu-item>
              <el-menu-item index="2-2">Option 2</el-menu-item>
            </el-menu-item-group>
            <el-menu-item-group title="Group 2">
              <el-menu-item index="2-3">Option 3</el-menu-item>
            </el-menu-item-group>
            <el-sub-menu index="2-4">
              <template #title>Option 4</template>
              <el-menu-item index="2-4-1">Option 4-1</el-menu-item>
            </el-sub-menu>
          </el-sub-menu>
            <el-sub-menu index="3">
            <template #title>
              <el-icon><setting /></el-icon>Navigator Three
            </template>
            <el-menu-item-group>
              <template #title>Group 1</template>
              <el-menu-item index="3-1">Option 1</el-menu-item>
              <el-menu-item index="3-2">Option 2</el-menu-item>
            </el-menu-item-group>
            <el-menu-item-group title="Group 2">
              <el-menu-item index="3-3">Option 3</el-menu-item>
            </el-menu-item-group>
            <el-sub-menu index="3-4">
              <template #title>Option 4</template>
              <el-menu-item index="3-4-1">Option 4-1</el-menu-item>
            </el-sub-menu>
          </el-sub-menu>
            <el-menu-item index="4">
            <el-icon><setting /></el-icon>
            <template #title>Navigator Four</template>
          </el-menu-item>
          </el-menu>
        </transition>

        <div class="toolbar">
          <el-button type="primary" @click="toggleCollapse">折叠/展开</el-button>
          <el-button type="primary" @click="toggleDirection">切换菜单方向</el-button>

          <el-dropdown>
            <el-icon style="margin-right: 8px; margin-top: 1px">
              <setting />
            </el-icon>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item>View</el-dropdown-item>
                <el-dropdown-item>Add</el-dropdown-item>
                <el-dropdown-item>Delete</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
          <span>Tom</span>
        </div>
      </el-header>

      <el-main>
        <el-scrollbar>
          <el-table :data="tableData">
            <el-table-column prop="date" label="Date" width="140" />
            <el-table-column prop="name" label="Name" width="120" />
            <el-table-column prop="address" label="Address" />
          </el-table>
        </el-scrollbar>
      </el-main>
    </el-container>
  </el-container>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { Menu as IconMenu, Message, Setting } from '@element-plus/icons-vue'

const activeIndex = ref('1')
// 默认为水平菜单，horizontal | vertical 前者为水平菜单，后者为垂直菜单
const direction = ref('horizontal')
const isVertical = ref(true) // 控制是否为垂直布局
// 切换菜单方向，同时整体切换菜单样式。同时添加过渡动画效果
const toggleDirection = () => {
  isVertical.value = !isVertical.value
}

// 是否水平折叠收起菜单（仅在 mode 为 vertical 时可用）
const isCollapsed = ref(false) // 控制菜单是否折叠
const toggleCollapse = () => {
  isCollapsed.value = !isCollapsed.value
  document.querySelector('.layout-container-demo .el-aside').style.width = isCollapsed.value ? '64px' : '250px'
}


const item = {
  date: '2016-05-02',
  name: 'Tom',
  address: 'No. 189, Grove St, Los Angeles',
}
const tableData = ref(Array.from({ length: 20 }).fill(item))

// 菜单激活回调，将激活的菜单背景色设置为指定颜色
const handleSelect = (key: string, keyPath: string[]) => {
  console.log(key, keyPath)
}
</script>

<style scoped lang="scss">
.layout-container-demo .el-header {
  position: relative;
  background-color: var(--el-color-primary-light-7);
  color: var(--el-text-color-primary);
}

// 侧边栏菜单 el-menu 动画自定义
.layout-container-demo .el-aside {
  color: var(--el-text-color-primary);
  background: var(--el-color-primary-light-8);

  // 此处给 el-aside 设置了初始默认宽度就不要在给 el-aside 设置 width: auto; 了，否则会导致菜单折叠时宽度的过渡动画失效。
  // 顶层容器宽度的过渡动画在使用 width: auto; 时并不生效。这是因为 CSS 中的过渡动画需要具体的数值进行计算，而 auto 是一个动态的、不可计算的值。
  width: 250px;
  transition: width 0.5s ease-in-out
}

.el-menu-demo {
  width: auto;  // 这里改为 auto 即可，不需要设置具体的宽度，由 el-aside 自适应。
  transition: width 0.5s ease-in-out;
}

// 将激活的菜单背景色设置为指定颜色
.el-menu-item.is-active {
  background-color: #d6edd6;
}

.layout-container-demo .toolbar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  right: 20px;
}

// 当 isVertical 为 true 时， el-aside 元素的宽度设置为 250px，并添加过渡动画效果
//.is-vertical .el-aside {
//  width: 200px;
//  transition: all 0.5s ease-in-out;
//}

// 当 isHorizontal 为 true 时， 该样式将被添加到 el-header 元素上
.is-horizontal {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 60px;
  background-color: var(--el-color-primary-light-7);
  color: var(--el-text-color-primary);
}





// 侧边栏菜单元素显示动画
.aside-menu-enter-active {
  animation: wipe-out-left 0.5s ease-in-out;
}
// 元素隐藏动画
.aside-menu-leave-active {
  animation: wipe-in-right 0.5s ease-in-out;
}
// 从右往左折叠菜单
@keyframes wipe-out-left {
  from {
    width: 0;
  }
  to {
    width: 250px;
  }
}
// 从左往右展开菜单
@keyframes wipe-in-right {
  from {
    width: 250px;
  }
  to {
    width: 0;
  }
}


// 头部菜单元素显示动画
.header-menu-enter-active {
  // animation: animation-name animation-duration animation-timing-function animation-delay animation-iteration-count animation-direction animation-fill-mode animation-play-state;
  animation: wipe-in-up 0.5s ease-in-out;
}
// 元素隐藏动画
.header-menu-leave-active {
  // ease-in-out 用于控制动画的速率，从而使动画更加平滑
  animation: wipe-out-down 0.5s ease-in-out;
}
// 从下往上折叠菜单
@keyframes wipe-in-up {
  from {
    opacity: 0; // 初始状态，透明度为 0
    transform: translateY(60px);
  }

  to {
    opacity: 1; // 结束状态，透明度为 1
    transform: translateY(0px);
  }
}
// 从上往下折叠菜单
@keyframes wipe-out-down {
  from {
    opacity: 1; // 结束状态，透明度为 1
    transform: translateY(0px);
  }

  to {
    opacity: 0; // 初始状态，透明度为 0
    transform: translateY(60px);
  }
}
</style>
